package com.example.onenetdht11;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.onenetdht11.entity.User2;

public class SetFragment extends Fragment {
    MainActivity mainActivity;
    User2 user;
    View view;
    EditText etDevice, etApiKey;
    TextView tvNickname, tvUsername, tvSex, tvAge, tvIDCard, tvAddress;
    Button btnSaveSettings;
    Button btnBackLogin;
    public SetFragment(){

    }
    public SetFragment(MainActivity mainActivity, User2 user){
        this.mainActivity = mainActivity;
        this.user = user;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_set,container,false);//第一个参数是布局文件名，第二个是父容器
        initView();
        initListener();
        initDate();
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            //GET_CODE是自定义的页面跳转识别码
            initView();
            initListener();
            initDate();
        }
    }

    @SuppressLint("CutPasteId")
    public void initView(){
        tvNickname = view.findViewById(R.id.tv_nickname);
        tvUsername = view.findViewById(R.id.tv_username);
        tvSex = view.findViewById(R.id.tv_sex);
        tvAge = view.findViewById(R.id.tv_age);
        tvIDCard = view.findViewById(R.id.tv_IDcard);
        tvAddress = view.findViewById(R.id.tv_address);
        etDevice = view.findViewById(R.id.et_device_id);
        etApiKey = view.findViewById(R.id.et_apikey);
        btnSaveSettings = view.findViewById(R.id.btn_save_settings);
        btnBackLogin = view.findViewById(R.id.btn_back_login);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View view) {
            String deviceid = etDevice.getText().toString();
            String apikey = etApiKey.getText().toString();
            Contests.DeviceID = deviceid;
            Contests.APIKey = apikey;
            switch (view.getId()) {
                case R.id.btn_save_settings:
                    gotoNextActivity(ReviseActivity.class,user);
                    break;
                case R.id.btn_back_login:
                    gotoNextActivity(LoginActivity.class);
                    break;
            }
        }
    };

    void initListener(){
        btnSaveSettings.setOnClickListener(listener);
        btnBackLogin.setOnClickListener(listener);
    }

    @SuppressLint("SetTextI18n")
    void initDate(){
        System.out.println("当前登录用户数据为：" + user.toString());
        int age = user.getAge();
        if (age == 0){
            tvAge.setText("未编辑");
        }else
            tvAge.setText(Integer.toString(age));
        if (user.getNickname() == null){
            tvNickname.setText("未编辑");
        }else
            tvNickname.setText(user.getNickname());
        if (user.getSex() == null){
            tvSex.setText("未编辑");
        }else
            tvSex.setText(user.getSex());
        if (user.getUsername() == null){
            tvUsername.setText("未编辑");
        }else
            tvUsername.setText(user.getUsername());
        if (user.getIdcard() == null){
            tvIDCard.setText("未编辑");
        }else
            tvIDCard.setText(user.getIdcard());
    }


    /**
     * 跳转到下一个界面
     * @param c -要跳转界面的类名
     */
    void gotoNextActivity(Class c){
        Intent intent_back = new Intent();
        intent_back.setClass(getActivity(),c);
        startActivity(intent_back);
        getActivity().finish();
    }

    /**
     * 带着user对象跳转到下一个界面
     * @param c 下一个界面的类对象
     * @param user  要传递的user对象
     */
    void gotoNextActivity(Class c,User2 user){
        Intent intent_back = new Intent();
        intent_back.setClass(getActivity(),c);
        Bundle bundle = new Bundle();
        bundle.putSerializable("user",user);
        intent_back.putExtras(bundle);
        startActivityForResult(intent_back,1);
    }
}
